﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;

namespace auxiliary_proj
{
    public partial class FormVendorUpdate : Form
    {
        string vendor_id;

        public FormVendorUpdate()
        {
            InitializeComponent();
        }
        bool isName = false, isAddress = false, isCity = false, isState = false;

        public FormVendorUpdate(string vendor_id, string name, string address, string location, string phone)
        {
            InitializeComponent();

            this.vendor_id = vendor_id;
            this.tbName.Text = name;
            this.tbAddress.Text = address;
            //this.tbLocation.Text = location;
            this.tbPhone.Text = phone;

            //split location into city, state zip
            string[] res1 = location.Split(',');
            tbCity.Text = res1[0];

            string[] res2 = res1[1].Trim().Split(' ');
            tbState.Text = res2[0];
            tbZipCode.Text = res2[1];

        }

        private void btnSubmit_Click(object sender, EventArgs e)
        {
            if ((isName) && (isAddress) && (isCity) && (isState))
            {
                string update = "UPDATE vendor SET name = '" + tbName.Text + "', address = '" + tbAddress.Text +"', location = '" + tbCity.Text + ", " + tbState.Text + " " + tbZipCode.Text +"', phone = '" + tbPhone.Text + "' where vendor_id = '" + vendor_id + "';";
                if (Query.nonQuery(update) == true)
                {
                    MessageBox.Show("Vendor Updated!", "Successful", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    Singleton.getUCVendors().viewVendors();
                    this.Dispose();
                }
                else MessageBox.Show("Failed to Update User!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else MessageBox.Show("Invalid Entry (*Missing Field(s))", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

        private void name_textChanged(object sender, EventArgs e)
        {
            isName = Parser.isMatch(sender, @"^[a-zA-Z\s-,&.]{3,}$");
        }

        private void address_textChanged(object sender, EventArgs e)
        {
            isAddress = Parser.isMatch(sender, @"^[a-zA-Z0-9\s-&.]{5,}$");
        }

        private void city_textChanged(object sender, EventArgs e)
        {
            isCity = Parser.isMatch(sender, @"^[a-zA-Z\s.-]{3,20}$");
        }

        private void state_textChanged(object sender, EventArgs e)
        {
            isState = Parser.isMatch(sender, @"^[a-zA-Z]{2}$");
        }
    }
}
